public class RobotIA extends Robot
{
	private static final int MAX_RECURSION = -1;

	public RobotIA(HistorizedLaby lab)
	{
		super(lab);
	}
	
	public int findExit()
	{
		if (work(0))
			return -1; //impossible
		else
		{
			HistorizedLaby hl = (HistorizedLaby)lab;
			hl.displayMoves();
			return hl.getMovesCount();
		}
	}
	
	//returns true if no path can be found
	private boolean work(int n)
	{
		if (lab.isExit())
			return false; //done, no error
		if (n == MAX_RECURSION)
			return true; //failed
		else
		{	
			//for each possible direction,
			for (int i=0; i<HistorizedLaby.MOVES.length; i++)
			{
				//try it if it's possible
				if (lab.go(HistorizedLaby.MOVES[i]))
				{
					//recursive call
					if (work(n+1))
						//backtrack
						((HistorizedLaby)lab).undo();
					else
						//done
						return false;
				}
			}
			//if we haven't returned from this function, 
			//there's no way to the exit from the current position.
			return true;
		}
	}
}
